package org.elephas.webapp.component.form;

import org.apache.wicket.Component;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.behavior.AbstractBehavior;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.resources.JavascriptResourceReference;

public class NicEditEnabler extends AbstractBehavior {

	private static final long serialVersionUID = 1L;

	private static final ResourceReference JS = new JavascriptResourceReference(
			NicEditEnabler.class, "res/nicedit.js");

	private Component component;

	@Override
	public void renderHead(IHeaderResponse response) {
		super.renderHead(response);
		StringBuffer sb = new StringBuffer();
		sb.append("new nicEditor().panelInstance('");
		sb.append(component.getMarkupId());
		sb.append("');");
		response.renderOnDomReadyJavascript(sb.toString());
	}

	@Override
	public void bind(Component component) {
		if (!(component instanceof TextArea)) {
			throw new IllegalStateException(
					"NicEdit can only be added to TextAreas");
		}

		if (this.component != null) {
			throw new IllegalStateException(
					"NicEdit can only be used for one TextArea");
		}
		component.setOutputMarkupId(true);
	}

}
